package com.licw.controller.user;


import com.licw.dto.ShoppingCartDTO;
import com.licw.entity.ShoppingCart;
import com.licw.result.Result;
import com.licw.service.ShoppingCartService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user/shoppingCart")
@ApiOperation("购物车接口")
public class ShoppingCartController {
    @Resource
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    @ApiOperation("添加到购物车")
    public Result<Object> add(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("添加到购物车:{}", shoppingCartDTO);
        shoppingCartService.add(shoppingCartDTO);
        return Result.success();

    }

    @PostMapping("/sub")
    @ApiOperation("删除购物车中一个商品")
    public Result<Object> sub(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("删除购物车中一个商品:{}", shoppingCartDTO);
        shoppingCartService.sub(shoppingCartDTO);
        return Result.success();

    }

    @GetMapping("/list")
    @ApiOperation("查看购物车")
    public Result<List<ShoppingCart>> list(){
        log.info("查看购物车");
        List<ShoppingCart> shoppes = shoppingCartService.list();
        return Result.success(shoppes);
    }


    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public Result<Object> clean(){
        log.info("清空购物车");
        shoppingCartService.deleteAll();
        return Result.success();
    }
}
